﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace FloatingWindowTools
{
    public class Window : IWin32Window
    {
        private readonly MapinfoWrapper mapinfo;
        public Window(MapinfoWrapper mapInfo, int windowID)
        {
            this.ID = windowID;
            this.mapinfo = mapInfo;
        }

        public int ID { get; set; }

        public IntPtr Handle
        {
            get
            {
                string handle = this.mapinfo.Eval(String.Format("WindowInfo({0},12)", this.ID));
                return new IntPtr(Convert.ToInt32(handle));
            }
        }

        public string Title
        {
            get
            {
                return this.mapinfo.Eval(String.Format("WindowInfo({0},1)", this.ID));
            }
        }

        public bool IsOpen
        {
            get
            {
                return (this.Handle != IntPtr.Zero);
            }
        }

        public int Width
        {
            get
            {
                RECT rec = new RECT();
                WinAPI.GetWindowRect(this.Handle, out rec);
                return (rec.Right - rec.Left);
            }
        }

        public int Height
        {
            get
            {
                RECT rec = new RECT();
                WinAPI.GetWindowRect(this.Handle, out rec);
                return (rec.Bottom - rec.Top);
            }
        }

        public void Maximize()
        {
            this.mapinfo.Eval(String.Format("Set Window {0} Max", this.ID));
        }

        private IntPtr parent;
        public IntPtr Parent
        {
            get
            {
                return this.parent;
            }
            set
            {
                WinAPI.SetParent(this.Handle, value);
                this.parent = value;
            }
        }

        public void RemoveCaptionBar()
        {
            int style = WinAPI.GetWindowLong(this.Handle, WinAPI.GWL_STYLE);
            WinAPI.SetWindowLong(this.Handle, WinAPI.GWL_STYLE, (style & ~WinAPI.WS_CAPTION));
        }

        public void RemoveBorder()
        {
            int style = WinAPI.GetWindowLong(this.Handle, WinAPI.GWL_STYLE);
            WinAPI.SetWindowLong(this.Handle, WinAPI.GWL_STYLE, (style & ~WinAPI.WS_THICKFRAME));
        }

        public void UpdateSize(int width, int height)
        {
            WinAPI.MoveWindow(this.Handle, 0, 0, width, height, false);
        }

        public void Close()
        {
            this.mapinfo.Do(String.Format("Close Window {0}", this.ID));
        }

        public IntPtr OwnerThreadId
        {
            get
            {
                return WinAPI.GetWindowThreadProcessId(this.Handle, new IntPtr(0));
            }
        }

        public enum WindowType
        {
            WIN_MAPPER = 1,
            WIN_BROWSER = 2,
            WIN_LAYOUT = 3,
            WIN_GRAPH = 4,
            WIN_BUTTONPAD = 19,
            WIN_TOOLBAR = 25,
            WIN_CART_LEGEND = 27,
            WIN_3DMAP = 28,
            WIN_ADORNMENT = 32,
            WIN_HELP = 1001,
            WIN_MAPBASIC = 1002,
            WIN_MESSAGE = 1003,
            WIN_RULER = 1007,
            WIN_INFO = 1008,
            WIN_LEGEND = 1009,
            WIN_STATISTICS = 1010,
            WIN_MAPINFO = 1011,
        }
    }
}
